
//
//  HFVisitorLoginView.swift
//  sinaMicroBlog
//
//  Created by mac on 15/11/10.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

protocol HFVisitorLoginViewDelegate: NSObjectProtocol {
    func visitorWillLogin()
    func visitorWillRegister()
}

class HFVisitorLoginView: UIView {
    
    weak var visitorDelegate: HFVisitorLoginViewDelegate?
    
    init() {
        super.init(frame: CGRectZero)
        setUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setUIInfo(ImageName imageName: String?, Title title: String) {
        iconView.hidden = false
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            iconView.hidden = true
            bringSubviewToFront(circleView)
        }else {
            startAnimaton()
        }
        tipLabel.text = title
    }
    //添加动画
    private func startAnimaton() {
        let animation = CABasicAnimation(keyPath:"transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.duration = 5
        animation.removedOnCompletion = false
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    //添加控件元素
    private func setUI() {
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //取消自动布局
        for view in subviews {
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        //手动约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 42))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView" : backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[registerButton]", options: [], metrics: nil, views: ["backView" : backView, "registerButton" : registerButton]))
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        loginButton.addTarget(self, action:"didClickLoginButton", forControlEvents: .TouchUpInside)
        registerButton.addTarget(self, action: "didClickRegisterButton", forControlEvents: .TouchUpInside)
    }
    
    //MARK: 懒加载所有控件

    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())
    
    private lazy var loginButton: UIButton = UIButton(title: "登录", backImage: "common_button_white_disable", titleColor: UIColor.lightGrayColor())
    @objc func didClickLoginButton() {
        visitorDelegate?.visitorWillLogin()
    }
    private lazy var registerButton: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", titleColor: UIColor.lightGrayColor())
    @objc func didClickRegisterButton() {
        visitorDelegate?.visitorWillRegister()
    }
    
}
